package per.dg.digital_klotski.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import per.dg.digital_klotski.R;
import per.dg.digital_klotski.utils.FontDisplayUtil;

public class DigitalKlotskiChildView extends View {

    Paint paint;

    String text;

    int textSize;

    int width;

    Bitmap bitmap;

    Paint strokePaint;

    public DigitalKlotskiChildView(Context context) {
        this(context, null);
    }

    public DigitalKlotskiChildView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DigitalKlotskiChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    private void initData() {
        paint = new Paint();

        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.box);

        textSize = FontDisplayUtil.dip2px(getContext(), 20);

        strokePaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int i = FontDisplayUtil.dip2px(getContext(), 11);
        width = getWidth();
        float textSize = width/7*4;
        Log.e("onDraw: ", i+":"+width/4);
        paint.setTextSize(textSize);
        paint.setTextAlign(Paint.Align.CENTER);
//


        paint.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/ALIBABA-PUHUITI-HEAVY.TTF"));

        //计算baseline
        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        float ascent = fontMetrics.ascent;
        float distance=(fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        float baseline=getHeight()/13*12/2+distance;
//                - FontDisplayUtil.dip2px(getContext(), 6);


//        canvas.drawLine(0, baseline-width/4*3, getWidth(), baseline-width/4*3, paint);
//        canvas.drawLine(0, baseline-paint.getTextSize()/3*2, getWidth(), baseline-paint.getTextSize()/3*2, paint);
//        canvas.drawLine(0, baseline+ascent, getWidth(), baseline+ascent, paint);
        Log.e("onDraw: ", ascent+"");
//        canvas.drawLine(0, baseline, getWidth(), baseline, paint);

        LinearGradient linearGradient = new LinearGradient(0, baseline-paint.getTextSize()/3*2, 0, baseline-paint.getTextSize()/3, getResources().getColor(R.color.shadow_start), getResources().getColor(R.color.shadow_center), Shader.TileMode.MIRROR);
        paint.setShader(linearGradient);

//        paint.setShadowLayer(10, 30, FontDisplayUtil.dip2px(getContext(), 11), getResources().getColor(R.color.shadow));

        canvas.drawText(text, getWidth()/2, baseline, paint);

        int i1 = FontDisplayUtil.dip2px(getContext(), 2);
        strokePaint.setStrokeWidth(i1);//设置描边的宽度
        strokePaint.setStyle(Paint.Style.STROKE);//设置画笔属性为描边
        strokePaint.setColor(getResources().getColor(R.color.stroke));
        strokePaint.setTextSize(i1+textSize);
        strokePaint.setTextAlign(Paint.Align.CENTER);
        strokePaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/ALIBABA-PUHUITI-HEAVY.TTF"));
        canvas.drawText(text, getWidth()/2, baseline, strokePaint);
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
        forceLayout();
        requestLayout();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        invalidate();
        forceLayout();
        requestLayout();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}
